//index.js
//获取应用实例
const app = getApp()
Page({
  data: {
    auth_img: '/images/auth_img.png',
    auth_text: '网络大闯关',
    auth_button: '确认授权登录',
    auth_info: '网络大闯关需要授权登录来获取你的微信基本信息，需要输入你的学号和真实姓名'
  },
  onLoad: function (options) {

    wx.checkSession({
      success() {
        console.log("wxSession成功！")
        // session_key 未过期，并且在本生命周期一直有效
        var openid = wx.getStorageSync('openid');
        wx.request({
          url: 'https://sdb.doways.cn/qa/HasUser.do',
          data: {
            wx_openid: openid
          },
          method: 'post',
          headers: {
            'Content-Type': 'application/json'
          },
          success: function (res) {
            console.log(res.data.result)
            if (res.data.result == 'ok') {
              wx.redirectTo({
                url: '/pages/list/list',
              })
            }
          },
          fail:function(){
            wx.showToast({
              title: '网络错误',
              icon: 'none',
              duration: 2000
            })
          }

        })
      },
      fail(){
        wx.showToast({
          title: '授权过期请点击“确认授权登录”',
          icon: 'none',
          duration: 2000
        })
      }
    })

    
  },
  onlogin: function(res) {
    wx.showToast({
      title: '登陆中',
      icon: 'loading',
      duration: 2000
    })
    wx.login({
      success(res) {
        if (res.code) {
          // 发起网络请求
          console.log('CODE:' + res.code)
          wx.request({
            url: 'https://sdb.doways.cn/qa/wxlogin.do',
            data: {
              jscode: res.code
            },
            method: 'post',
            headers: {
              'Content-Type': 'application/json'
            },
            success: function(jj) {
              wx.showToast({
                title: '登录成功',
                icon: 'success',
                duration: 2000
              })
              if (jj.data.result == 'ok') {
                var obj = JSON.parse(jj.data.info); 
                console.log('登录成功！' + jj.data.result)
                wx.setStorageSync('login_status', jj.data.result)
                wx.setStorageSync('openid', obj.openid)
                console.log(obj.openid)
                console.log(wx.getStorageSync('login_status'))
                console.log(wx.getStorageSync('openid'))
                var openid = wx.getStorageSync('openid');
                console.log("OPENID:"+openid)
                wx.request({
                  url: 'https://sdb.doways.cn/qa/HasUser.do',
                  data: {
                    wx_openid: openid
                  },
                  method: 'post',
                  headers: {
                    'Content-Type': 'application/json'
                  },
                  success: function (res) {
                    console.log(res.data.result)
                    if (res.data.result == 'none') {
                      wx.redirectTo({
                        url: '/pages/userbind/userbind',
                      })
                    }else{
                      wx.redirectTo({
                        url: '/pages/list/list',
                      })
                    }
                  },
                  fail: function () {
                    wx.showToast({
                      title: '网络错误',
                      icon: 'none',
                      duration: 2000
                    })
                  }

                })
                

                // wx.removeStorageSync('login_status')
              } else {
                wx.showToast({
                  title: '登录失败',
                  icon: 'none',
                  duration: 2000
                })
                console.log('登录失败！' + res.errMsg)
              }
            },
            fail: function () {
              wx.showToast({
                title: '网络错误',
                icon: 'none',
                duration: 2000
              })
            }
          })
          
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
    


  },

})